import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../res/resources.dart';

typedef ItemBuilder<T> = Widget Function(BuildContext context,T item,int index,bool isSelected);

class SelectItemWidget<T> extends StatelessWidget {
  var position = 0;
  final List<T> items;
  final ItemBuilder<T> itemBuilder;
  IndexedWidgetBuilder? separatorBuilder;
  Axis scrollDirection;
  bool shrinkWrap;
  EdgeInsets padding;

  Function(int index, T item)? onCallback;

  SelectItemWidget(this.items,
      { required this.itemBuilder,this.position = 0,
        this.onCallback,
        this.separatorBuilder,
        this.scrollDirection = Axis.vertical,
        this.shrinkWrap = false,this.padding = EdgeInsets.zero});

  @override
  Widget build(BuildContext context) {
    return StatefulBuilder(builder: (context, setState) {
      return ListView.separated(
        itemBuilder: (context, index) {
          return itemBuilder(context,items[index],index,position==index).onTap(() {
            setState(() {
              position = index;
            });
            if (onCallback != null) {
              onCallback!(index, items[index]);
            }
          },highlightColor: Colors.transparent,splashColor: Colors.transparent);
        },
        separatorBuilder: (context, index) {
          return separatorBuilder == null
              ? Container()
              : separatorBuilder!(context, index);
        },
        itemCount: items.length,
        scrollDirection: scrollDirection,
        shrinkWrap: shrinkWrap,
        padding: padding,
      );
    });
  }
}

Widget titleImage(String type, bool selected) {
  if (type == "1") {
    return Images.breakfastImage(selected);
  }

  if (type == "2") {
    return Images.lunchImage(selected);
  }

  if (type == "3") {
    return Images.dinnerImage(selected);
  }

  return Container();
}
